package learner.dtw;

import java.util.List;

import learner.DTWCostCalculator;
import learner.ModelLearner;
import learner.Pattern;
import learner.PatternModel;

public class DTWLearner implements ModelLearner {

	private DTWCostCalculator calc = new DTWCostCalculator();
	@Override
	public PatternModel learnModel(String name, List<Pattern> patterns) {
		Double maxDistance = 0.0;
		Double totalDistance = 0.0;
		int nbDistances = 0;
		for(int i=0;i<patterns.size()-1;i++){
			for(int j =i+1;j<patterns.size();j++){
				Double newDistance = calc.calculateCost(patterns.get(i), patterns.get(j));
				if(newDistance>maxDistance)
					maxDistance= newDistance;
				totalDistance+=newDistance;
				nbDistances++;
			}
		}
		double averageDist= totalDistance/nbDistances;
		//return new DTWModel(maxDistance);
		return new DTWModel(averageDist);
	}

}
